package com.lantosec.hmrp.modules.archives.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantosec.hmrp.core.shiro.TokenUtil;
import com.lantosec.hmrp.core.util.DateUtils;
import com.lantosec.hmrp.core.util.UUIDUtils;
import com.lantosec.hmrp.modules.archives.entity.UserTrain;
import com.lantosec.hmrp.modules.archives.mapper.UserTrainMapper;
import com.lantosec.hmrp.modules.archives.service.IUserTrainService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.archives.vo.UserTrainVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import lombok.extern.log4j.Log4j2;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 培训信息表 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-06-09
 */
@Service
@Primary
@Log4j2
public class UserTrainServiceImpl extends ServiceImpl<UserTrainMapper, UserTrain> implements IUserTrainService {

    @Override
    public void saveUserTrain(UserTrainVO vo) {
        UserTrain userTrain = vo.convertToEntity();
        userTrain.setUserTrainId(UUIDUtils.get());
        //获取当前用户id和名称
        String token = TokenUtil.getToken();
        userTrain.setCreateId(TokenUtil.getUserId());
        userTrain.setCreateUser(TokenUtil.getName());
        userTrain.setCreateTime(DateUtils.nowDate());
        userTrain.setVerifyStatus(1);
        this.save(userTrain);
    }

    @Override
    @Transactional
    public void updateUserTrain(UserTrainVO vo) {
        String userTrainId = vo.getUserTrainId();
        //新增一条数据
        //saveUserTrain(vo);
        //修改这条数据的审核状态为2
        if(StringUtils.isNotEmpty(userTrainId)){
            UserTrain userTrain = vo.convertToEntity();
            userTrain.setVerifyStatus(1);
            this.updateById(userTrain);
        }else {
            saveUserTrain(vo);
        }
    }

    @Override
    public List<UserTrain> getUserTrain(String userId) {
        List<UserTrain> userTrainList = this.list(new QueryWrapper<UserTrain>().eq(UserTrain.ColName.user_id.toString(),userId).orderByDesc(UserTrain.ColName.create_time.toString()));
        return userTrainList;
    }

    @Override
    public List<UserTrain> getUserTrainVerify(String userId) {
        List<UserTrain> userTrainList = this.list(new QueryWrapper<UserTrain>().eq(UserTrain.ColName.user_id.toString(),userId).in(UserTrain.ColName.verify_status.toString(), 0, 2));
        return userTrainList;
    }

    @Override
    public void verifyUserTrain(UserTrainVO vo) {
        UserTrain userTrain = vo.convertToEntity();
        Integer verifyStatus = userTrain.getVerifyStatus();
        //如果审核不通过
        if (vo.getState() == 0) {
            //审核状态为审核中的变成 已审核
            if (verifyStatus == 2) {
                userTrain.setVerifyStatus(1);
            }
            //审核状态为未审核的变成 审核不通过
            if (verifyStatus == 0) {
                userTrain.setVerifyStatus(3);
            }
        }
        //如果审核通过
        if (vo.getState() == 1) {
            //审核状态为审核中的变为 历史记录
            if (verifyStatus == 2) {
                userTrain.setVerifyStatus(4);
            }
            //审核状态为未审核的变为 已审核
            if (verifyStatus == 0) {
                userTrain.setVerifyStatus(1);
            }
        }
        //获取当前用户id和名称
        userTrain.setVerifyId(TokenUtil.getUserId());
        userTrain.setVerifyUser(TokenUtil.getName());
        userTrain.setVerifyTime(DateUtils.nowDate());
        this.updateById(userTrain);
    }

    @Override
    public List<UserTrain> getUserTrainHistory(String userId) {
        List<UserTrain> userTrainList = this.list(new QueryWrapper<UserTrain>().eq(UserTrain.ColName.user_id.toString(),userId).eq(UserTrain.ColName.verify_status.toString(), 4)
                                                                                .orderByDesc(UserTrain.ColName.create_time.toString()));
        return userTrainList;
    }
}
